// pages/login/main.js
wx.cloud.init();
const db = wx.cloud.database();

Page({

    /**
     * 页面的初始数据
     */
    data: {
        isShow: false,
        isPassword: true,
        isSend: true,
        usermessage: [],
        phone: "",
        code: 12345,
        iscode: null,
        usermes: "",
        password: "",
        flag: false,
        htmlSnip: `<p class="login-text">若手机号未注册将进入注册流程，注册即为同意 <a href="" class="jindonga">《京东用户注册协议》</a><a href="" class="jindonga">《京东隐私政策》</a></p>`
    },
    sendcode() {
        let random_num = parseInt(Math.random() * 100000);
        let phonestyle = /^[1][3,4,5,7,8,9][0-9]{9}$/;
        if (!phonestyle.test(this.data.phone)) {
            wx.showToast({
                title: '请输入正确的手机号码',
                icon: 'none',
                duration: 1500,
            });
        } else {
            this.setData({
                isSend: !this.data.isSend,
                code: random_num
            })
        }

    },

    showPassword() {
        let isPassword = !this.data.isPassword;
        this.setData({
            isPassword: isPassword
        })
    },
    loginStyle() {
        let isShow = !this.data.isShow;
        this.setData({
            isShow: isShow
        })

    },
    phoneinput(e) {
        this.setData({
            phone: e.detail.value
        })
    },
    codeinput(e) {
        this.setData({
            iscode: e.detail.value
        })
    },
    usermesinput(e) {
        this.setData({
            usermes: e.detail.value
        })
    },
    passwordinput(e) {
        this.setData({
            password: e.detail.value
        })
    },
    submitphco() {
        console.log(this.data.usermessage);
        if (this.data.code == this.data.iscode) {
            for (let i = 0; i < this.data.usermessage.length; i++) {
                console.log(this.data.usermessage[i].loginaccount);
                console.log("111");
                
                if (this.data.phone == this.data.usermessage[i].loginaccount) {
                    console.log("校验成功");
                    let userinfo = this.data.usermessage[i];
                    wx.setStorageSync("userinfo", userinfo);
                    wx.switchTab({
                        url: '/pages/myPage/main',
                    });
                    this.setData({
                        flag: true
                    })
                }
            }
            if (!this.data.flag) {
                wx.showModal({
                    title: '京东注册',
                    content: '该手机号还未注册，是否跳转到注册页面',
                    showCancel: true,
                    cancelText: '否',
                    cancelColor: '#000000',
                    confirmText: '是',
                    confirmColor: ' #ff4714',
                    success: (result) => {
                        if (result.confirm) {
                            let phone = this.data.phone;
                            console.log(phone);
                            console.log(this.data.phone)
                            wx.setStorageSync("phone", phone);
                            wx.redirectTo({
                                url: '/pages/register/possword',
                            });
                        }
                    },
                });
            }
        } else {
            wx.showToast({
                title: '验证码错误，请重新输入',
                icon: 'none',
                duration: 1500,
            });
            this.setData({
                iscode: ""
            })
        }
    },
    submit() {
        if (this.data.usermes.length == 0 || this.data.password.length == 0) {
            wx.showToast({
                title: '请输入正确的用户名、密码',
                icon: 'none',
                duration: 1500,
            });
        } else {
            for (let i = 0; i < this.data.usermessage.length; i++) {
                if ((this.data.usermes == this.data.usermessage[i].username) || (this.data.usermes == this.data.usermessage[i].loginaccount)) {
                    if (this.data.password == (this.data.usermessage[i].loginpassword)) {
                        console.log("校验成功");
                        this.setData({
                            flag:true
                        })
                        let userinfo= this.data.usermessage[i];
                        wx.setStorageSync("userinfo", userinfo);
                        wx.switchTab({
                            url: '/pages/myPage/main',
                            success: (result) => {}
                        });
                        break;
                    } else {
                        this.setData({
                            password: ""
                        })
                        wx.showToast({
                            title: '密码错误',
                            icon: 'none',
                            duration: 1500,
                        });
                    }
                }

                 
            }
           if(!this.data.flag){
            console.log(this.data.usermes)
            wx.showToast({
                title: '用户名输入错误或不存在，请重新输入',
                icon: 'none',
                duration: 1500,
            });
            this.setData({
                usermes: ""
            })
           }
           
        }
    },
    handleGetUserInfo(e) {
        console.log(e);
        
          let  userInfo = e.detail;
          console.log(userInfo)
        wx.setStorageSync("userinfo", userInfo);
        wx.switchTab({
            url: '/pages/myPage/main',
            success: (result) => {}
        });
    },
    // },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.huoqu();
        console.log("111" == 111);
    },

    async huoqu() {
        let res = await (await db.collection('user').get())
        console.log(res)
        this.setData({
            usermessage: res.data
        })
        console.log(this.data.usermessage)
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})